// Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
// Copyright (C) 2010  Winch Gate Property Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#include <nel/misc/debug.h>
#include <nel/misc/config_file.h>
#include <nel/misc/displayer.h>
#include <nel/misc/command.h>
#include <nel/misc/log.h>
#include <nel/misc/window_displayer.h>
#include <nel/misc/path.h>
#include <server_share/server_def.h>

#include "persistant_data_service.h"

#include "db_manager.h"
#include "db_manager_messages.h"

#ifdef NL_OS_WINDOWS
#	define NOMINMAX
#	include <windows.h>
#endif // NL_OS_WINDOWS

using namespace std;
using namespace NLMISC;
using namespace NLNET;

TUnifiedCallbackItem	DbManagerCbArray[] =
{
	{ "DB_PUT",			&cbPut },
	{ "DB_GET",			&cbGet },
	{ "DB_DEL",			&cbDel },
	{ "ERR",			&cbErr },
};

NLMISC::CFileDisplayer *Fd = NULL;
NLMISC::CStdDisplayer Sd;

void	CPersistantDataService::init()
{
	std::string fn = IService::getInstance()->SaveFilesDirectory;
	fn += ConfigFile.getVar("LogDir").asString();
	fn += "persistant_game_service.stat";
	nlinfo("Persistant Data Serivce stat in directory '%s'", fn.c_str());
	Fd = new NLMISC::CFileDisplayer(fn);
	getGlobal_Loger().addDisplayer (Fd);
	if (WindowDisplayer) getGlobal_Loger().addDisplayer (WindowDisplayer);

	std::string DatabaseName = NLNET::IService::getInstance()->ConfigFile.getVar("DatabaseName").asString ();
	std::string DatabaseHost = NLNET::IService::getInstance()->ConfigFile.getVar("DatabaseHost").asString ();
	std::string DatabaseLogin = NLNET::IService::getInstance()->ConfigFile.getVar("DatabaseLogin").asString ();
	std::string DatabasePassword = NLNET::IService::getInstance()->ConfigFile.getVar("DatabasePassword").asString ();

	nlinfo("DatabaseName '%s'", DatabaseName.c_str());
	nlinfo("DatabaseHost '%s'", DatabaseHost.c_str());
	nlinfo("DatabaseLogin '%s'", DatabaseLogin.c_str());
	nlinfo("DatabasePassword '%s'", DatabasePassword.c_str());

//	CUnifiedNetwork::getInstance()->setServiceDownCallback("*", onServiceDown);
//	NLNET::CUnifiedNetwork::getInstance()->setServiceDownCallback("EGS", cbEGSDisconnection, NULL);

	CDbManager::getInstance().init( DatabaseHost, DatabaseLogin, DatabasePassword, DatabaseName );

	getGlobal_Loger().displayNL ("Persistant Data Service initialized");
}

/*
 * Release
 */
void	CPersistantDataService::release()
{
	CDbManager::getInstance().release();
}

/*
 * Update
 */
bool	CPersistantDataService::update()
{
	if (!CDbManager::getInstance().update())
		return false;

	return true;
}


NLNET_SERVICE_MAIN( CPersistantDataService, "PDS", "persistant_data_service", 50002, DbManagerCbArray, "", "" );



